from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui

import sys

class QSliderTest(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(QSliderTest, self).__init__(parent)

        self.is_slider_move = False

        self.setWindowTitle("QSliderTest例子")
        self.resize(300, 100)

        self.label_show_slider_value = QtWidgets.QLabel("20")
        self.label_show_slider_value.setAlignment(QtCore.Qt.AlignCenter)

        self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
        self.slider.setMinimum(10)
        self.slider.setMaximum(50)
        self.slider.setSingleStep(2)
        self.slider.setValue(20)
        self.slider.setTickPosition(QtWidgets.QSlider.TicksBelow)
        self.slider.setTickInterval(2)
        self.slider.valueChanged.connect(self.slider_value_changed)
        self.slider.sliderReleased.connect(self.slider_released)
        self.slider.sliderPressed.connect(self.slider_pressed)

        _v_box_layout = QtWidgets.QVBoxLayout()
        _v_box_layout.addWidget(self.label_show_slider_value)
        _v_box_layout.addWidget(self.slider)

        self.setLayout(_v_box_layout)

    def slider_value_changed(self):
        if not self.is_slider_move:
            _current_value = self.slider.value()
            self.label_show_slider_value.setText("%d" % _current_value )

    def slider_released(self):
        self.is_slider_move = False
        self.slider_value_changed()
        print("用户释放slider : ", self.slider.value())

    def slider_pressed(self):
        self.is_slider_move = True
        print("用户按下slider : ", self.slider.value())


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    win = QSliderTest()
    win.show()
    sys.exit(app.exec_())